#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")

#define ID_USERNAME 1
#define ID_PASSWORD 2
#define ID_LOGIN_BUTTON 3

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    static TCHAR szAppName[] = TEXT("LoginWindow");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("本程序需要Windows NT"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, TEXT("用户登录界面"), WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
                        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndUsername, hwndPassword, hwndLoginButton;
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message)
    {
    case WM_CREATE:
        hwndUsername = CreateWindow(TEXT("EDIT"), NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER,
                                    80, 30, 200, 25,
                                    hwnd, (HMENU)ID_USERNAME, NULL, NULL);

        hwndPassword = CreateWindow(TEXT("EDIT"), NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD,
                                    80, 70, 200, 25,
                                    hwnd, (HMENU)ID_PASSWORD, NULL, NULL);

        hwndLoginButton = CreateWindow(TEXT("BUTTON"), TEXT("登录"),
                                       WS_CHILD | WS_VISIBLE,
                                       100, 110, 100, 30,
                                       hwnd, (HMENU)ID_LOGIN_BUTTON, NULL, NULL);
        return 0;

    case WM_SIZE: // 处理窗口大小变化
    {
        int width = LOWORD(lParam);
        // 根据窗口大小调整输入框和按钮的位置和大小
        MoveWindow(hwndUsername, 80, 30, width - 100, 25, TRUE);
        MoveWindow(hwndPassword, 80, 70, width - 100, 25, TRUE);
        MoveWindow(hwndLoginButton, (width - 100) / 2, 110, 100, 30, TRUE);
    }
        return 0;

    case WM_COMMAND:
        if (LOWORD(wParam) == ID_LOGIN_BUTTON)
        {
            TCHAR username[50], password[50];
            GetWindowText(hwndUsername, username, sizeof(username) / sizeof(TCHAR));
            GetWindowText(hwndPassword, password, sizeof(password) / sizeof(TCHAR));

            if (lstrcmp(username, TEXT("admin")) == 0 && lstrcmp(password, TEXT("password")) == 0)
            {
                MessageBox(hwnd, TEXT("登录成功！"), TEXT("提示"), MB_OK);
            }
            else
            {
                MessageBox(hwnd, TEXT("用户名或密码错误！"), TEXT("提示"), MB_OK | MB_ICONERROR);
            }
        }
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        
        // 在适当的位置绘制标签
        TextOut(hdc, 50, 10, TEXT("用户名:"), lstrlen(TEXT("用户名:")));
        TextOut(hdc, 50, 80, TEXT("密码:"), lstrlen(TEXT("密码:"))); // 增加间距
        
        EndPaint(hwnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}
